from setting import *
from animation import *


class Text(pygame.sprite.Sprite):
    """a pygame Sprite displaying text"""

    def __init__(self,
                 msg="ok",
                 color=(0, 0, 0),
                 topleft=(0, 0)):
        self.groups = selectgroup
        self.topleft = topleft
        self._layer = 99
        pygame.sprite.Sprite.__init__(self, self.groups)
        self.newmsg(msg, color)

    def update(self):
        pass  

    def newmsg(self, msg, color=(0, 0, 0)):
        self.image = self.write(msg, color)
        self.rect = self.image.get_rect()
        self.rect.topleft = self.topleft

    def write(self, msg="ok", color=(0, 0, 0)):
        """write text into pygame surfaces"""
        myfont = pygame.font.SysFont("None", 32)
        mytext = myfont.render(msg, True, color)
        mytext = mytext.convert_alpha()
        return mytext

    def set_pos(self, x, y):
        self.rect.x = x
        self.rect.y = y


class IntroduceVideo(pygame.sprite.Sprite):
    def __init__(self, index):
        self.groups = selectgroup
        self._layer = 99
        pygame.sprite.Sprite.__init__(self, self.groups)
        self.animation = Animation()
        self.images = self.animation.load_image(
            'images/hero/plane' + str(index) + '.png', 226, 85, 1, 4)
        original = self.images[0]
        self.image = pygame.transform.scale2x(original)
        self.rect = self.image.get_rect()
        self.index = 0
        self.heor_info = {
            0: ['speed 4', 'damage 4', 'HP 100'],
            1: ['speed 3', 'damage 5', 'HP 100'],
            2: ['speed 5', 'damage 3', 'HP 100'],
            3: ['speed 5', 'damage 5', 'HP 50']
        }
        self.set_animation(0)
        plane_info = self.heor_info[index]
        self.screentext0 = Text("plane information", (152, 0, 255), (SCENEWIDTH//2+100, 200))
        self.screentext1 = Text(plane_info[0], (255, 0, 255), (SCENEWIDTH//2+100, 250))
        self.screentext2 = Text(plane_info[1], (255, 0, 0), (SCENEWIDTH//2+100, 300))
        self.screentext3 = Text(plane_info[2], (0, 255, 0), (SCENEWIDTH//2+100, 350))

    def update(self):
        original = self.animation.get_current_image()
        self.image = pygame.transform.scale2x(original)
        plane_info = self.heor_info[self.index]
        self.screentext1.newmsg(plane_info[0], (255, 0, 255))
        self.screentext2.newmsg(plane_info[1], (255, 0, 0))
        self.screentext3.newmsg(plane_info[2], (0, 255, 0))

    def set_pos(self, x, y):
        self.rect.x = x
        self.rect.y = y

    def set_animation(self, index):
        original_center = self.rect.center
        self.images = self.animation.load_image(
            'images/hero/plane' + str(index) + '.png', 226, 85, 1, 4)
        original = self.images[0]
        self.image = pygame.transform.scale2x(original)
        self.rect.center = original_center
        self.index = index


class SelectMenu(pygame.sprite.Sprite):
    def __init__(self, imagename):
        self._layer = 3
        self.groups = selectgroup
        pygame.sprite.Sprite.__init__(self, self.groups)
        self.image = pygame.image.load(imagename)
        self.rect = self.image.get_rect()

    def update(self):
        pass

    def move(self, direction, distant=100):
        if direction == 'LEFT':
            self.rect.x -= distant
        elif direction == 'RIGHT':
            self.rect.x += distant

    def set_pos(self, x, y):
        self.rect.x = x
        self.rect.y = y


class SelectHero(pygame.sprite.Sprite):
    def __init__(self, scene):
        self._layer = 3
        self.groups = selectgroup
        pygame.sprite.Sprite.__init__(self, self.groups)
        self.main_scene = scene
        self.image = pygame.image.load('images/bg/select.jpeg')
        self.rect = self.image.get_rect()
        self.hero_index = 0
        self.first_index = 0
        self.last_index = 3
        self.selected = False
        #飞机图像位置
        pos_x = SCENEWIDTH // 2 - 200
        pos_y = SCENEHEIGHT * 3 // 4
        step = 100
        hero0 = SelectMenu('images/hero/plane0_sel.png')
        hero0.set_pos(pos_x, pos_y)
        pos_x += step
        hero1 = SelectMenu('images/hero/plane1_sel.png')
        hero1.set_pos(pos_x, pos_y)
        pos_x += step
        hero2 = SelectMenu('images/hero/plane2_sel.png')
        hero2.set_pos(pos_x, pos_y)
        pos_x += step
        hero3 = SelectMenu('images/hero/plane3_sel.png')
        hero3.set_pos(pos_x, pos_y)
        #飞机选择框的图像
        self.select_frame = SelectMenu("images/hero/plane_sel.png")
        self.select_frame.rect.center = hero0.rect.center

        Text("PRESS ENTER TO CONFIRM", (182, 192, 192), (SCENEWIDTH //2-150, 100))
        Text('PRESS A OR D TO SELECT', (182, 182, 182), (SCENEWIDTH //2-150, 130))

        self.intro_fram = IntroduceVideo(0)
        self.intro_fram.set_pos(SCENEWIDTH//2-100, 200)

    def kill(self):
        selectgroup.empty()

    def run(self):
        while not self.selected:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    exit()
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_j:
                        pass
                    elif event.key == pygame.K_a:
                        if self.hero_index == self.first_index:
                            break
                        self.select_frame.move('LEFT')
                        self.hero_index -= 1
                        self.intro_fram.set_animation(self.hero_index)
                    elif event.key == pygame.K_d:
                        if self.hero_index == self.last_index:
                            break
                        self.hero_index += 1
                        self.select_frame.move('RIGHT')
                        self.intro_fram.set_animation(self.hero_index)
                    elif event.key == pygame.K_RETURN:  #语句中的retur居然也会触发这个,只好改用break
                        self.selected = True
                    #ESC 退出
                    elif event.key == pygame.K_ESCAPE or event.key == pygame.K_F4:
                        pygame.quit()
                        exit()
            selectgroup.update()
            selectgroup.draw(self.main_scene)
            pygame.display.update()